package com.zhp.aop;

import com.zhp.anno.TenantIgnore;
import com.zhp.tenant.TenantContextHolder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;

/**
 * @author zhanghp
 * @since 2024/5/16 11:08
 */
@Slf4j
@Aspect
@Component
public class TenantIgnoreAspect {

    @SneakyThrows
    @Around("@within(ignore) || @annotation(ignore)")
    public Object around(ProceedingJoinPoint point, TenantIgnore ignore) {
        if (ignore == null) {
            Class<?> aClass = point.getTarget().getClass();
            ignore = AnnotationUtils.findAnnotation(aClass, TenantIgnore.class);
        }
        if (ignore != null) {
            TenantContextHolder.skip();
        }
        Object proceed = point.proceed();
        if (!ignore.globalFlag()) {
            TenantContextHolder.noSkip();
        }
        return proceed;
    }
}
